﻿using System.DirectoryServices.Protocols;
using AutoMapper;
using B.S.Production.Domain.Entities;
using B.S.Production.ErrorCode;
using B.S.Production.Write.API.Application.Command.ProductionOrderCommand;
using MediatR;
using SqlSugar;
using Yitter.IdGenerator;

namespace B.S.Production.Write.API.Application.CommandHandlers.ProductionOrderCommandHandler
{
    public class UpdateProductionOrderCommandHandler : IRequestHandler<UpdateProductionOrderCommand, ApiResult<int>>
    {
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        private readonly ISqlSugarClient db;

        public UpdateProductionOrderCommandHandler(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateProductionOrderCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var entity = mapper.Map<ProductionOrder>(request);
            result.Data = await db.Updateable<ProductionOrder>(entity).ExecuteCommandAsync();
            if (result.Data > 0)
            {
                result.Code = ApiEnums.修改成功;
                result.Msg = ApiEnums.修改成功.ToString();
            }
            else
            {
                result.Code = ApiEnums.修改失败;
                result.Msg = ApiEnums.修改失败.ToString();
            }
            return result;
        }
    }
}
